package com.service.impl;

import com.bean.Enterprise;
import com.bean.Page;
import com.dao.EnterpriseDao;
import com.dao.impl.EnterpriseDaoImpl;
import com.service.EnterpriseService;

import java.util.List;

public class EnterpriseServiceImpl implements EnterpriseService {

    private EnterpriseDao enterpriseDao=new EnterpriseDaoImpl();
    @Override
    public Enterprise login(Enterprise enterprise) {
        return enterpriseDao.queryEnterpriseByEnterpriseidAndPassword(enterprise.getEnterpriseid(),enterprise.getPassword());
    }

    @Override
    public boolean existsEnterpriseid(String enterpriseid) {
        if(enterpriseDao.existsEnterpriseid(enterpriseid)==null){
            return false;
        }
        return true;
    }

    @Override
    public void register(Enterprise enterprise) {
        enterpriseDao.saveEnterprise(enterprise);
    }



    @Override
    public void updatePass(String enterpriseid, String password) {
        enterpriseDao.updatePass(enterpriseid,password);
    }

    @Override
    public void updateMess(String enterpriseid, String enterprise_name, String relname, String telephone, String address) {

        enterpriseDao.updateMess(enterpriseid, enterprise_name,relname, telephone, address);
    }

    @Override
    public void modifyPhoto(String enterpriseid, String head_sculpture) {

        enterpriseDao.updatePhoto(enterpriseid,head_sculpture);
    }

    @Override
    public void delete(String enterpriseid) {
        enterpriseDao.delete(enterpriseid);
    }

    @Override
    public Page<Enterprise> queryPageEnterprise(int pageNo, int pageSize) {
        Page<Enterprise>page=new Page<Enterprise>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示的数量
        page.setPageSize(pageSize);

        //求总记录数
        Integer pageTotalCount=enterpriseDao.queryForPageTotalCount();
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码
        page.setPageTotal(pageTotal);
        //求当前页数据开始的索引
        int begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<Enterprise> items=enterpriseDao.queryForPageItem(begin,pageSize);
        //设置当前页数据
        page.setItem(items);

        return page;
    }

    @Override
    public Enterprise showMess(String enterpriseid) {
        return enterpriseDao.showMess(enterpriseid);
    }
}
